package com.xqgl.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.xqgl.bean.Admin;
import com.xqgl.dao.AdminDao;
import com.xqgl.util.ConnertionUtil;

public class AdminDaoImp implements AdminDao{
	Connection conn = ConnertionUtil.getConnection();
	PreparedStatement ps;
	Statement statement;
	
	public ArrayList<Admin> getAllUserInfos() {
		ArrayList<Admin> admins = new ArrayList<>();
		String sql = "select * from admin ";
		try {
			ps = conn.prepareStatement(sql);
			ResultSet rst = ps.executeQuery(sql);
			while(rst.next()) {
				Admin admin = new Admin();
				admin.setId(rst.getInt("id"));
				admin.setAdminname(rst.getString("adminname"));
				admin.setAdminpassword(rst.getString("adminpassword"));
				admin.setAdminautograph(rst.getString("adminautograph"));
				admin.setAdminphone(rst.getInt("adminphone"));
				admin.setAdminemail(rst.getString("adminemail"));
				admin.setAdminpower(rst.getString("adminpower"));
				admin.setAdminctime(rst.getString("adminctime"));
				admin.setAdminstate(rst.getString("adminstate"));
				admins.add(admin);
			}		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admins;
	}
	
//	@Override
//	public int add(Admin admin) throws SQLException {
//		String sql = "insert into admin(adminname,adminpassword,adminphone,adminemail,adminpower,adminctime,adminstate)"
//				+ "values(?,?,?,?,?,?,?)";
//		ps = conn.prepareStatement(sql);
//		ps.setString(1, admin.getAdminname());
//		ps.setString(2, admin.getAdminpassword());
//		ps.setInt(3, admin.getAdminphone());
//		ps.setString(4, admin.getAdminemail());
//		ps.setString(5, admin.getAdminpower());
//		ps.setString(6, admin.getAdminctime());
//		ps.setString(7, admin.getAdminstate());
//		int a = ps.executeUpdate();
//		ps.close();
//		return a;
//	}

	@Override
	public int update(Admin admin) throws SQLException {
		String sql = "update admin set adminname = '"+admin.getAdminname()+"', adminpassword = '"+admin.getAdminpassword()+"', adminautograph = '"+admin.getAdminautograph()+"', adminphone = "+admin.getAdminphone()+" where adminname = '"+admin.getAdminname()+"'";
		ps = conn.prepareStatement(sql);
		int b = ps.executeUpdate();
		ps.close();
		return b;
	}

//	@Override
//	public boolean delete(int id) throws SQLException {
//		String sql = "delete from admin where id = "+id+"";
//		statement = conn.createStatement();
//		boolean c = statement.execute(sql);
//		statement.close();
//		return !c;
//	}

	@Override
	public Admin findById(String name) throws SQLException {
		Admin admin = new Admin();
		String sql = "select * from admin where id = ?";
		ps = conn.prepareStatement(sql);
		ps.setString(1, name);
		ResultSet set = ps.executeQuery();
		while(set.next()){
			admin.setId(set.getInt("id"));
			admin.setAdminname(set.getString("adminname"));
			admin.setAdminpassword(set.getString("adminpassword"));
			admin.setAdminautograph(set.getString("adminautograph"));
			admin.setAdminphone(set.getInt("adminphone"));
//			admin.setAdminemail(set.getString("adminemail"));
//			admin.setAdminpower(set.getString("adminpower"));
//			admin.setAdminctime(set.getString("adminctime"));
//			admin.setAdminstate(set.getString("adminstate"));
		}
		ps.close();
		return admin;
	}
	
//	@Override
//	public int update(Admin admin) throws SQLException {
//		String sql = "update admin set adminname = '"+admin.getAdminname()+"', adminpassword = '"+admin.getAdminpassword()+"', adminphone = '"+admin.getAdminphone()+"',"
//				+ "admincount = '"+admin.getAdmincount()+"' where adminname = '"+admin.getAdminname()+"'";
//		ps = conn.prepareStatement(sql);
//		int d = ps.executeUpdate();
//		ps.close();
//		return d;
//	}

}
